package com.ruoyi.web.controller.appcontroller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Message;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mapper.PageMapper;
import com.ruoyi.system.mapper.MessageMapper;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;


@RestController
@RequestMapping("/user")
@Slf4j
public class AppController {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private PageMapper pageMapper;
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        Long userId = SecurityUtils.getUserId();
//        System.out.println("aaa");
        SysUser sysUser = userService.selectUserById(userId);
        return  AjaxResult.success(sysUser);

    }
    //获取前端红点  status 0表示页面没有消息 status1表示页面有消息
    @GetMapping("getMessageCount/{pageId}")
    public AjaxResult getMessageCount(@PathVariable("pageId") Long pageId)
    {
        log.info(SecurityUtils.getUsername()+"获取页面状态");
        int status=pageMapper.getPageStatus(pageId);
        return  AjaxResult.success(status);
    }


    @GetMapping("clearMessage")
    public AjaxResult clearMessage()
    {
        log.info(SecurityUtils.getUsername()+"删除所有的message");
        Long userId = SecurityUtils.getUserId();
        messageMapper.clearMessage(userId);
        return  AjaxResult.success();
    }




    //消息推送
    @GetMapping("/addMessageCount/{pageId}")
    public AjaxResult addMessageCount(@PathVariable("pageId") Long pageId)
    {
       log.info("消息推送");
        pageMapper.updatePageStatus(pageId);
        return  AjaxResult.success();
    }


    @Scheduled(cron = "0 0 0 * * ?", zone = "Asia/Shanghai")  // Singapore=UTC+8
    public void fixedRateTask() {
        log.info("定时任务将消息清零");
        pageMapper.initPageStauts();
    }

}
